package com.dhcc.bpm.modules.develop.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.dhcc.bpm.modules.develop.entity.Datasource;
import com.dhcc.bpm.modules.develop.service.IDatasourceService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.ApiParam;
import lombok.AllArgsConstructor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springblade.core.boot.ctrl.BladeController;
import org.springblade.core.launch.constant.AppConstant;
import org.springblade.core.log.annotation.ApiLog;
import org.springblade.core.mp.support.Condition;
import org.springblade.core.mp.support.Query;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.utils.Func;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.List;

/**
 * @ClassName DatasourceController
 * @Description 数据源配置表 控制器
 * @Author YaoZC
 * @Date 2020/01/19 15:32
 * @Version 1.0
 * @Copyright: 2020 东华软件 . All rights reserved.
 */
@RestController
@AllArgsConstructor
@RequestMapping(AppConstant.APPLICATION_DEVELOP_NAME + "/datasource")
@Api(value = "数据源配置表", tags = "数据源配置表接口")
public class DatasourceController extends BladeController {
    private final static Logger log = LoggerFactory.getLogger(DatasourceController.class);
    private IDatasourceService datasourceService;

    /**
     * @param datasource
     * @return org.springblade.core.tool.api.R<com.dhcc.bpm.modules.develop.entity.Datasource>
     * @throws
     * @Description 详情
     * @Author xiafayu
     * @Date 2020/2/24 20:11
     **/
    @ApiLog("详情")
    @GetMapping("/detail")
    @ApiOperationSupport(order = 1)
    @ApiOperation(value = "详情", notes = "传入datasource")
    public R<Datasource> detail(Datasource datasource) {
        Datasource detail = datasourceService.getOne(Condition.getQueryWrapper(datasource));
        return R.data(detail);
    }

    /**
     * @param datasource
     * @param query
     * @return org.springblade.core.tool.api.R<com.baomidou.mybatisplus.core.metadata.IPage < com.dhcc.bpm.modules.develop.entity.Datasource>>
     * @throws
     * @Description 分页 数据源配置表
     * @Author xiafayu
     * @Date 2020/2/24 20:11
     **/
    @ApiLog("分页")
    @GetMapping("/list")
    @ApiOperationSupport(order = 2)
    @ApiOperation(value = "分页", notes = "传入datasource")
    public R<IPage<Datasource>> list(Datasource datasource, Query query) {
        IPage<Datasource> pages = datasourceService.page(Condition.getPage(query), Condition.getQueryWrapper(datasource));
        return R.data(pages);
    }

    /**
     * @param datasource
     * @return org.springblade.core.tool.api.R
     * @throws
     * @Description 新增 数据源配置表
     * @Author xiafayu
     * @Date 2020/2/24 20:12
     **/
    @ApiLog("新增")
    @PostMapping("/save")
    @ApiOperationSupport(order = 4)
    @ApiOperation(value = "新增", notes = "传入datasource")
    public R save(@Valid @RequestBody Datasource datasource) {
        return R.status(datasourceService.save(datasource));
    }

    /**
     * @param datasource
     * @return org.springblade.core.tool.api.R
     * @throws
     * @Description 修改 数据源配置表
     * @Author xiafayu
     * @Date 2020/2/24 20:12
     **/
    @ApiLog("修改")
    @PostMapping("/update")
    @ApiOperationSupport(order = 5)
    @ApiOperation(value = "修改", notes = "传入datasource")
    public R update(@Valid @RequestBody Datasource datasource) {
        return R.status(datasourceService.updateById(datasource));
    }

    /**
     * @param datasource
     * @return org.springblade.core.tool.api.R
     * @throws
     * @Description 新增或修改 数据源配置表
     * @Author xiafayu
     * @Date 2020/2/24 20:12
     **/
    @ApiLog("新增或修改")
    @PostMapping("/submit")
    @ApiOperationSupport(order = 6)
    @ApiOperation(value = "新增或修改", notes = "传入datasource")
    public R submit(@Valid @RequestBody Datasource datasource) {
        datasource.setUrl(datasource.getUrl().replace("&amp;", "&"));
        return R.status(datasourceService.saveOrUpdate(datasource));
    }


	/**
	 * @Description 删除 数据源配置表
	 * @Author xiafayu
	 * @Date 2020/2/24 20:12
	 * @param ids
	 * @return org.springblade.core.tool.api.R
	 * @throws
	 **/
	@ApiLog("删除")
	@PostMapping("/remove")
	@ApiOperationSupport(order = 7)
	@ApiOperation(value = "逻辑删除", notes = "传入ids")
	public R remove(@ApiParam(value = "主键集合", required = true) @RequestParam String ids) {
		return R.status(datasourceService.deleteLogic(Func.toLongList(ids)));
	}

    /**
     * @param
     * @return org.springblade.core.tool.api.R<java.util.List < com.dhcc.bpm.modules.develop.entity.Datasource>>
     * @throws
     * @Description 数据源列表
     * @Author xiafayu
     * @Date 2020/2/24 20:12
     **/
    @ApiLog("下拉数据源")
    @GetMapping("/select")
    @ApiOperationSupport(order = 8)
    @ApiOperation(value = "下拉数据源", notes = "查询列表")
    public R<List<Datasource>> select() {
        List<Datasource> list = datasourceService.list();
        return R.data(list);
    }

    /**
     * @param
     * @return org.springblade.core.tool.api.R
     * @throws
     * @Description 测试连接
     * @Author xiafayu
     * @Date 2020/2/24 20:12
     **/
    @ApiLog("测试连接")
    @PostMapping("/getConnect")
    @ApiOperationSupport(order = 8)
    @ApiOperation(value = "测试连接", notes = "查询列表")
    public R getConnect(@Valid @RequestBody Datasource datasource) {
        //1.加载驱动程序
        try {
            Class.forName(datasource.getDriverClass());
            //2. 获得数据库连接
            DriverManager.setLoginTimeout(3);
            datasource.setUrl(datasource.getUrl().replace("&amp;", "&"));
            log.info("连接数据库：{}", datasource.getUrl());
            Connection connection = DriverManager.getConnection(datasource.getUrl(), datasource.getUsername(), datasource.getPassword());
            if (connection != null) {
                return R.status(true);
            }
        } catch (ClassNotFoundException e) {
            log.error("数据库驱动错误:{}", e);
        } catch (SQLException e) {
            log.error("数据库连接异常:{}", e);
        }

        return R.status(false);
    }
}
